package com.bzkj.basicinfo.service.impl.warehouse;

import com.bzkj.basicinfo.domain.warehouse.PdaWarehouseOutBillsList;
import com.bzkj.basicinfo.domain.warehouse.PdaWarehouseOutBillsDetail;
import com.bzkj.basicinfo.mapper.PdaWarehouseOutBillsMapper;
import com.bzkj.basicinfo.service.warehouse.IPdaWarehouseOutBillsService;
import com.bzkj.common.constant.BillStatusConstant;
import com.bzkj.common.core.domain.model.LoginUser;
import com.bzkj.common.utils.DateUtils;
import com.bzkj.common.utils.SecurityUtils;
import com.bzkj.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 出库单据Service业务层处理
 * 
 * @author system
 * @date 2025-10-17
 */
@Service
public class PdaWarehouseOutBillsServiceImpl implements IPdaWarehouseOutBillsService {
    
    @Resource
    private PdaWarehouseOutBillsMapper pdaWarehouseOutBillsMapper;

    /**
     * 查询出库单据列表
     * 
     * @param billsList 出库单据主表
     * @return 出库单据
     */
    @Override
    public List<PdaWarehouseOutBillsList> selectPdaWarehouseOutBillsList(PdaWarehouseOutBillsList billsList) {
        return pdaWarehouseOutBillsMapper.selectPdaWarehouseOutBillsList(billsList);
    }

    /**
     * 查询出库单据详情
     * 
     * @param billsID 出库单据主键
     * @return 出库单据
     */
    @Override
    public PdaWarehouseOutBillsList selectPdaWarehouseOutBillsByBillsID(String billsID) {
        return pdaWarehouseOutBillsMapper.selectPdaWarehouseOutBillsByBillsID(billsID);
    }

    /**
     * 新增出库单据
     * 
     * @param billsList 出库单据
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPdaWarehouseOutBillsList(PdaWarehouseOutBillsList billsList) {
        String currentTime = DateUtils.getTime();
        String currentUser = SecurityUtils.getUsername();
        String currentUserName = SecurityUtils.getLoginUser().getUser().getNickName();

        billsList.setCheckDate(currentTime);
        billsList.setUserId(currentUser);
        // 设置制单人信息
        billsList.setCreatorId(currentUser);
        billsList.setCreatorName(currentUserName);
        billsList.setCreateTime(DateUtils.getNowDate());
        // 出库单状态：1-待拣货
        billsList.setBillsState(BillStatusConstant.OUT_PENDING_PICK);

        int rows = pdaWarehouseOutBillsMapper.insertPdaWarehouseOutBillsList(billsList);
        insertPdaWarehouseOutBillsDetail(billsList);
        return rows;
    }

    /**
     * 修改出库单据
     * 
     * @param billsList 出库单据
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePdaWarehouseOutBillsList(PdaWarehouseOutBillsList billsList) {
        // 删除原有明细
        pdaWarehouseOutBillsMapper.deletePdaWarehouseOutBillsDetailByBillsID(billsList.getBillsId());
        // 新增明细
        insertPdaWarehouseOutBillsDetail(billsList);
        // 修改主表
        return pdaWarehouseOutBillsMapper.updatePdaWarehouseOutBillsList(billsList);
    }

    /**
     * 批量删除出库单据
     * 
     * @param billsIDs 需要删除的出库单据主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePdaWarehouseOutBillsByBillsIDs(String[] billsIDs) {
        return pdaWarehouseOutBillsMapper.deletePdaWarehouseOutBillsByBillsIDs(billsIDs);
    }

    /**
     * 删除出库单据信息
     * 
     * @param billsID 出库单据主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePdaWarehouseOutBillsByBillsID(String billsID) {
        return pdaWarehouseOutBillsMapper.deletePdaWarehouseOutBillsByBillsID(billsID);
    }

    /**
     * 新增出库单据明细信息
     *
     * @param billsList 出库单据对象
     */
    private void insertPdaWarehouseOutBillsDetail(PdaWarehouseOutBillsList billsList) {
        List<PdaWarehouseOutBillsDetail> detailList = billsList.getOutDetailList();
        String billsId = billsList.getBillsId();
        if (StringUtils.isNotNull(detailList) && detailList.size() > 0) {
            for (PdaWarehouseOutBillsDetail detail : detailList) {
                detail.setBillsId(billsId);
            }
            pdaWarehouseOutBillsMapper.batchInsertPdaWarehouseOutBillsDetail(detailList);
        }
    }

    /**
     * 制单操作
     *
     * @param billsList 出库单据
     * @return 结果
     */
    @Transactional
    @Override
    public int createBill(PdaWarehouseOutBillsList billsList) {
        return insertPdaWarehouseOutBillsList(billsList);
    }

    /**
     * 拣货操作（支持FIFO先进先出）
     *
     * @param billsID 单据号
     * @param pickingUserId 拣货人ID
     * @param pickingUserName 拣货人姓名
     * @return 结果
     */
    @Transactional
    @Override
    public int pickingGoods(String billsID, String pickingUserId, String pickingUserName) {
        String currentTime = DateUtils.getTime();

        PdaWarehouseOutBillsList billsList = new PdaWarehouseOutBillsList();
        billsList.setBillsId(billsID);
        billsList.setPickingUserId(pickingUserId);
        billsList.setPickingUserName(pickingUserName);
        billsList.setPickingTime(currentTime);
        // 设置出库单状态：3-待复核
        billsList.setBillsState(BillStatusConstant.OUT_PICKED);

        return pdaWarehouseOutBillsMapper.updatePdaWarehouseOutBillsList(billsList);
    }

    /**
     * 复核操作
     *
     * @param billsID 单据号
     * @param reviewUserId 复核人ID
     * @param reviewUserName 复核人姓名
     * @return 结果
     */
    @Transactional
    @Override
    public int reviewGoods(String billsID, String reviewUserId, String reviewUserName) {
        String currentTime = DateUtils.getTime();

        PdaWarehouseOutBillsList billsList = new PdaWarehouseOutBillsList();
        billsList.setBillsId(billsID);
        billsList.setReviewUserId(reviewUserId);
        billsList.setReviewUserName(reviewUserName);
        billsList.setReviewTime(currentTime);
        // 设置出库单状态：4-复核完成待出库
        billsList.setBillsState(BillStatusConstant.OUT_REVIEWED);

        return pdaWarehouseOutBillsMapper.updatePdaWarehouseOutBillsList(billsList);
    }

    /**
     * 出库操作
     *
     * @param billsID 单据号
     * @param outUserId 出库人ID
     * @param outUserName 出库人姓名
     * @return 结果
     */
    @Transactional
    @Override
    public int deliverGoods(String billsID, String outUserId, String outUserName) {
        String currentTime = DateUtils.getTime();

        PdaWarehouseOutBillsList billsList = new PdaWarehouseOutBillsList();
        billsList.setBillsId(billsID);
        billsList.setOutUserId(outUserId);
        billsList.setOutUserName(outUserName);
        billsList.setOutTime(currentTime);
        // 设置出库单状态：5-已出库
        billsList.setBillsState(BillStatusConstant.OUT_DELIVERED);

        // TODO: 这里应该扣减库存
        // 调用库存服务扣减库存

        return pdaWarehouseOutBillsMapper.updatePdaWarehouseOutBillsList(billsList);
    }
}

